package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.CarBean;
import com.project.bean.ManBean;
import com.project.mapper.ICarMapper;
import com.project.mapper.IManMapper;
import com.project.service.IManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ManServiceImpl implements IManService {
    @Autowired
    private IManMapper manMapper;
    @Autowired
    private ICarMapper carMapper;

    @Override
    public List<ManBean> findAll() {
        return manMapper.findAll();
    }

    @Override
    public void add(ManBean man, List<CarBean> carList) {
        manMapper.insert(man);
        manMapper.addCarList(man.getId(),carList);
    }

    @Override
    public void delCaseCade(Integer id) {
        QueryWrapper<CarBean> qw = new QueryWrapper<>();
        qw.eq("fk_manId",id);
        carMapper.delete(qw);
        manMapper.deleteById(id);
    }

    @Override
    public void delSetNull(Integer id) {
        manMapper.delSetNull(id);
    }

    @Override
    public ManBean findById(Integer id) {
        ManBean man = manMapper.selectById(id);
        QueryWrapper<CarBean> qw = new QueryWrapper<>();
        qw.eq("fk_manId",id);
        man.setCarList(carMapper.selectList(qw));

        return man;
    }
}
